package com.zijiebutiao.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijiebutiao.pojos.Param;
import com.zijiebutiao.pojos.TCheckgroup;
import com.zijiebutiao.pojos.TCheckgroupCheckitem;
import com.zijiebutiao.pojos.TCheckitem;
import com.zijiebutiao.service.TCheckgroupCheckitemService;
import com.zijiebutiao.service.TCheckgroupService;
import com.zijiebutiao.service.TCheckitemService;
import com.zijiebutiao.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Date 2023/2/28 16:34
 * @Author 干饭
 * @Email 1229219472@qq.com
 * @Version v1.0
 */
@RestController
@RequestMapping("/checkitem")
@Api(tags = "检查项管理")
public class TCheckitemController {
    @Autowired
    private TCheckitemService tCheckitemService;
    @Autowired
    private TCheckgroupCheckitemService service;
    @Autowired
    private TCheckgroupService tCheckgroupService;

    @PostMapping("/findPage.do")
    @ApiOperation(value = "分页查询",notes = "传递pararm对象其中查询条件可以为空")
    public ResultVo getAll(@RequestBody Param param) {
        ResultVo resultVo = new ResultVo();
        Integer currentPage = Integer.valueOf(param.getCurrentPage());
        Integer pageSize = Integer.valueOf(param.getPageSize());
        System.out.println(param);
        if (("").equals(currentPage) || currentPage == 0) {
            currentPage = 1;
        }
        if (("").equals(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        System.out.println(param.getQueryString()+"============================"+param.getQueryString());

        if (Objects.isNull(param.getQueryString())||("").equals(param.getQueryString())) {
            IPage<TCheckitem> page = new Page<>(currentPage, pageSize);
            resultVo.setData(tCheckitemService.page(page));
            return resultVo;
        } else {
            IPage<TCheckitem> page = new Page<>(currentPage, pageSize);
            Wrapper<TCheckitem> qw = Wrappers.lambdaQuery(TCheckitem.class)
                    .eq(TCheckitem::getCode, param.getQueryString())
                    .or().eq(TCheckitem::getName, param.getQueryString());
            TCheckitem one = tCheckitemService.getOne(qw);
            long count = tCheckitemService.count(qw);
            System.out.println(count);
            System.out.println(count);
            if (count < 1) {
                IPage<TCheckitem> page1 = new Page<>(currentPage, pageSize);
                resultVo.setData(tCheckitemService.page(page1));
                resultVo.setCode(403);
                resultVo.setErrorMsg("项目编码或项目名称不存在");
                return resultVo;
            }
            page.setRecords(Arrays.asList(one));
            page.setCurrent(count);
            page.setSize(pageSize);
            page.setPages(currentPage);
            page.setTotal(count);

            resultVo.setData(page);
        }
        return resultVo;
    }

    @Transactional
    @PostMapping("/add.do")
    @ApiOperation(value = "检查项添加",notes = "传递TCheckitem的json数据实现添加")
    public ResultVo addTCheckitem(@RequestBody TCheckitem item) {
        ResultVo resultVo = new ResultVo();
        LambdaQueryWrapper<TCheckitem> qw = new LambdaQueryWrapper<>();
        qw.eq(TCheckitem::getCode, item.getCode());
        TCheckitem byId = tCheckitemService.getOne(qw, false);
        System.out.println(byId);
        if (byId == null) {
            tCheckitemService.save(item);
            resultVo.setCode(200);
            resultVo.setErrorMsg("添加成功");
        } else {
            resultVo.setErrorMsg("项目编码已存在，不能重复");
            resultVo.setCode(403);
        }
        return resultVo;
    }


    @Transactional
    @PostMapping("/edit.do")
    @ApiOperation(value = "检查项修改",notes = "传递TCheckitem对象并携带id")
    public ResultVo updateTCheckitem(@RequestBody TCheckitem item) {
        ResultVo resultVo = new ResultVo();
        TCheckitem tCheckitem = tCheckitemService.getById(item.getId());
        Wrapper<TCheckitem> wq = Wrappers.lambdaQuery(TCheckitem.class)
                .ne(TCheckitem::getCode, tCheckitem.getCode());
        for (TCheckitem checkitem : tCheckitemService.list(wq)) {
            if (checkitem.getCode().equals(item.getCode())) {
                resultVo.setCode(403);
                resultVo.setErrorMsg("编码重复修改失败");
                return resultVo;
            }
        }
        Wrapper<TCheckitem> qw = Wrappers.lambdaQuery(TCheckitem.class)
                .eq(TCheckitem::getId, item.getId());
        tCheckitemService.update(item, qw);
        resultVo.setErrorMsg("修改成功");
        return resultVo;
    }

    @Transactional
    @GetMapping("/delete.do")
    @ApiOperation(value = "删除",notes = "携带id进行删除，必须在检查组删除完后检查项才能删除")
    public ResultVo removeTCheckitem(String id) {
        System.out.println("=======================");
        ResultVo resultVo = new ResultVo();
        TCheckitem byId = tCheckitemService.getById(id);
        Wrapper<TCheckgroupCheckitem> qw = Wrappers.lambdaQuery(TCheckgroupCheckitem.class)
                .eq(TCheckgroupCheckitem::getCheckitemId, id);
        long count = service.count(qw);
        System.out.println("count==" + count + "================================================");

        if (count == 0) {
            if (byId != null) {
                tCheckitemService.removeById(id);
                resultVo.setErrorMsg("删除成功");
            } else {
                resultVo.setErrorMsg("删除失败");
                resultVo.setCode(403);
            }
        } else {
            resultVo.setErrorMsg("请先删除已绑定的检查组");
            resultVo.setCode(403);
            return resultVo;
        }
        return resultVo;
    }


    @GetMapping("/findById.do")
    @ApiOperation(value = "通过id进行查询")
    public ResultVo getById(String id) {
        System.out.println(id);
        ResultVo resultVo = new ResultVo();
        TCheckitem tCheckitem = tCheckitemService.getById(id);
        if (tCheckitem != null) {
            resultVo.setData(tCheckitem);
        } else {
            resultVo.setErrorMsg("该项目不存在");
        }
        return resultVo;
    }

    @GetMapping("/findAll.do")
    @ApiOperation(value = "通过检查组查询检查项的内容")
    public ResultVo findAll() {
        ResultVo resultVo = new ResultVo();
        List<TCheckitem> list = tCheckitemService.list();
        resultVo.setData(list);
        return resultVo;
    }

    @GetMapping("/findCheckItemIdsByCheckGroupId.do")
    @ApiOperation(value = "通过检查组的id判断检查项是否存在")
    public ResultVo findCheckItemIdsByCheckGroupId(String checkgroupId) {
        ResultVo resultVo = new ResultVo();
        TCheckgroup byId = tCheckgroupService.getById(checkgroupId);
        if (Objects.isNull(byId)) {
            Wrapper<TCheckgroupCheckitem> qw = Wrappers.lambdaQuery(TCheckgroupCheckitem.class)
                    .eq(TCheckgroupCheckitem::getCheckgroupId, checkgroupId);
            TCheckgroupCheckitem one = service.getOne(qw);
            TCheckitem tCheckitem = tCheckitemService.getById(one.getCheckitemId());
            resultVo.setData(tCheckitem);
        } else {
            resultVo.setErrorMsg("检查项不存在");
            resultVo.setCode(403);
        }
        return resultVo;
    }
}
